package login

import (
	"context"
	"strconv"

	"algo-sdk-go/project/matchmaking/matchMaking-api/internal/svc"
	"algo-sdk-go/project/matchmaking/matchMaking-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoadUserTokenLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoadUserTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoadUserTokenLogic {
	return &LoadUserTokenLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoadUserTokenLogic) LoadUserToken(req *types.LoadUserTokenRequest) (resp *types.LoadUserTokenResponse, err error) {
	token, err := l.svcCtx.LoginService.LoadLocalCacheResult(l.ctx, strconv.Itoa(req.UserNum), req.CacheFlush, 0)
	if err != nil {
		return nil, nil
	}

	resp = &types.LoadUserTokenResponse{
		Data: types.LoadUserTokenData{
			Token: strconv.Itoa(token),
		},
	}
	return
}
